package com.zqx.controller;

import com.zqx.domain.Board;
import com.zqx.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("board")
public class BoardController {
    @Autowired
    private BoardService boardService;
    @RequestMapping("listBoard")
    public String listBoard(Model model){
        List<Board> list = boardService.list(null);
        model.addAttribute("boardList", list);
        return "admin-board-list";
    }
    @RequestMapping("delBoard/{id}")
    public String delBoard(@PathVariable Integer id){
        boolean b = boardService.removeById(id);
        return "redirect:/board/listBoard";
    }
    @RequestMapping("saveBoard")
    public String saveBoard(Board board, HttpSession session){
        String currentUser = (String) session.getAttribute("currentUser");
        String email = (String) session.getAttribute("email");
        String image = (String) session.getAttribute("image");
        board.setBtime(new Date());
        board.setSname(currentUser);
        board.setSimage(image);
        board.setEmail(email);
        boolean save = boardService.save(board);
        return "redirect:/board/listBoard";
    }
}
